﻿package com.fecesaids.greatgame.inventory {
	import flash.text.*;
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.geom.*;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	class ItemMenu extends Sprite{
		private var _optionArray:Array;
		private var backGround:Sprite;
		private var backGroundDrop:Sprite;
		private var selectionRectangle:Sprite;
		private var maxWidth:int;
		private var maxHeight:int;
		private var curSelectedFunction:String;
		private var myObjectRef:InventoryItem;
		public function ItemMenu(optionArray:Array, thisObjectRef:InventoryItem):void{
			myObjectRef = thisObjectRef;
			_optionArray = optionArray;
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseClick);
			var textFieldArray:Array = new Array();
			var optionArrayLength:int = optionArray.length;
			maxHeight = 0;
			maxWidth = 0;
			var i:int;
			for(i=0;i<optionArrayLength;i++){
				textFieldArray.push(new TextField());
				textFieldArray[i].text = optionArray[i];
				textFieldArray[i].autoSize = TextFieldAutoSize.LEFT;
				textFieldArray[i].selectable = false;
				
				if(textFieldArray[i].width > maxWidth)
					maxWidth = textFieldArray[i].width;
				if(textFieldArray[i].height > maxHeight)
					maxHeight = textFieldArray[i].height;
				
				addChild(textFieldArray[i]);
			}
			for(i=0;i<optionArrayLength;i++){
				textFieldArray[i].x = 3;
				textFieldArray[i].y = i*maxHeight;
			}
			backGround = new Sprite();
			backGroundDrop = new Sprite();
			selectionRectangle = new Sprite();
			addChildAt(backGroundDrop, 0);
			addChildAt(backGround, 1);
			addChildAt(selectionRectangle, 2);
			//backGround.graphics.lineStyle(2, 0x000000);
			backGround.graphics.beginFill(0x999999);
			backGround.graphics.drawRect(0, 0, maxWidth + 10, (maxHeight * optionArrayLength));
			backGround.graphics.endFill();
			backGroundDrop.graphics.beginFill(0x000000);
			backGroundDrop.graphics.drawRect(3, 3, maxWidth + 10, (maxHeight * optionArrayLength));
			backGroundDrop.graphics.endFill();
			backGroundDrop.alpha = .5;
			selectionRectangle.graphics.beginFill(0x777777);
			selectionRectangle.graphics.drawRect(0, 0, maxWidth + 10, maxHeight);
			selectionRectangle.graphics.endFill();
		}
		public function getWidth():int{
			return backGround.width;
		}
		public function getHeight():int{
			return backGround.height;
		}
		public function onMouseMove(event:MouseEvent):void{
			var mouseYTemp = mouseY;
			if(mouseY >= backGround.height){
				mouseYTemp = backGround.height-1;
			}
			selectionRectangle.x = 0;
			selectionRectangle.y = (int)((mouseYTemp) / maxHeight) * maxHeight;
		}
		public function onMouseClick(event:MouseEvent):void{
			stage.focus=stage;
			var mouseYTemp = mouseY;
			if(mouseY >= backGround.height){
				mouseYTemp = backGround.height-1;
			}
			var itemNumber:int = ((int)(mouseYTemp/maxHeight));
			curSelectedFunction = _optionArray[itemNumber];
			this.dispatchEvent(new Event("menuItemClicked"));
		}
		public function getDoFunction():String{
			return curSelectedFunction;
		}
		public function getMyObjectRef():InventoryItem{
			return myObjectRef;
		}
	}
}